<?php

class Wallcontroller{

	public function __construct( Registry $registry, $directCall=true )
	{
		$this->registry = $registry;
		if( $this->registry->getObject('authenticate')->isLoggedIn() )
		{
			$urlBits = $this->registry->getObject('url')->getURLBits();
			$profile = $registry->getObject('authenticate')->getUser()->getCurrentProfile();
			$CurrentProfileID = $profile->getProfileID();
			$userID = $registry->getObject('authenticate')->getUser()->getUserID();

			if (isset($urlBits[1]))
			{
				switch($urlBits[1])
				{
					case 'post':
						{
							if (isset($_POST['update']))
							{
								$this->registry->getObject('template')->buildFromTemplates( 'wall/wallmsgs.tpl.php');
								require_once( FRAMEWORK_PATH . 'models/Wall.php');
								$wall = new Wall($this->registry);

								$update = $_POST['update'];
								$data = $wall->InsertUpdate($userID, $profile, $update);
								$cachedata = $this->registry->getObject('db')->cacheData( $data );

								$this->registry->getObject('template')->getPage()->addTag( 'status_msgs', array( 'DATA', $cachedata ) );


								break;
							}
						}
						break;
					case 'comment':
						{
							if (isset($_POST['comment']) && isset($_POST['msg_id']))
							{
								$this->registry->getObject('template')->buildFromTemplates( 'wall/wallcomments.tpl.php');
								require_once( FRAMEWORK_PATH . 'models/Wall.php');
								$wall = new Wall($this->registry);

								$comment = $_POST['comment'];
								$status_id = $_POST['msg_id'];

								$data = $wall->InsertComment($userID, $CurrentProfileID, $status_id, $comment, $profile);
								$cachedata = $this->registry->getObject('db')->cacheData( $data );
								$this->registry->getObject('template')->getPage()->addTag('status_id', $status_id);
								$this->registry->getObject('template')->getPage()->addPPTag( 'wallcomments'.$status_id, array( 'DATA', $cachedata ));
							}

						}
						break;
					case 'deletecomment':
						{
							if (isset($_POST['com_id']))
							{
								require_once( FRAMEWORK_PATH . 'models/Wall.php');
								$wall = new Wall($this->registry);
								echo $wall->DeleteComment($_POST['com_id'], $profile);
							}
						}
						break;
					case 'deletestatus':
						{
							if (isset($_POST['msg_id']))
							{
								require_once( FRAMEWORK_PATH . 'models/Wall.php');
								$wall = new Wall($this->registry);
								echo $wall->DeleteUpdate($_POST['msg_id'], $profile);
							}
						}
						break;
					case 'allcomments':
						{
							if (isset($_POST['msg_id']))
							{
								$this->registry->getObject('template')->buildFromTemplates( 'wall/wallcomments.tpl.php');
								require_once( FRAMEWORK_PATH . 'models/Wall.php');
								$wall = new Wall($this->registry);
								$comments = $wall->Comments($_POST['msg_id'], $userID, $CurrentProfileID, $profile, true);
								if ($comments)
								{
									$cachedata = $this->registry->getObject('db')->cacheData( $comments );
									$this->registry->getObject('template')->getPage()->addTag('status_id', $_POST['msg_id']);
									$this->registry->getObject('template')->getPage()->addPPTag( 'wallcomments'.$_POST['msg_id'], array( 'DATA', $cachedata ));
								}
								else
								{

								}
							}
						}
						break;
					case 'autoload':
						{

						}
						break;
					case 'likes':
						{
							if (isset($_POST['msg_id']) && isset($_POST['likes']))
							{
								$msgid = $_POST['msg_id'];
								$like = $_POST['likes']; //like = 1 unlike = 0

								require_once( FRAMEWORK_PATH . 'models/Wall.php');
								$wall = new Wall($this->registry);
								if ($like == 1)
								$text = $wall->InsertLike($_POST['msg_id'], $userID, $CurrentProfileID);
								else
								$text = $wall->DeleteLike($msgid, $userID, $CurrentProfileID);

								$iliked = $wall->getILiked();
								$likesCount = $wall->getLikesCount();

								$this->registry->getObject('template')->buildFromTemplates('wall/wall_likes.tpl.php');

								$this->registry->getObject('template')->getPage()->addPPTag('status_id', $_POST['msg_id']);
								$this->registry->getObject('template')->getPage()->addPPTag('text', $text);
								$this->registry->getObject('template')->getPage()->addPPTag('likescount', $likesCount);

								$this->registry->getObject('template')->parseOutput();
								$content = $this->registry->getObject('template')->getPage()->getContent();
								$content = '<DATA>'.$content.'</DATA>';
								$this->registry->getObject('template')->getPage()->setContent($content);

								echo '<LIKECOUNT>'.$likesCount.'</LIKECOUNT>';
							}
						}
						break;
					case 'loadmore':
						{
							$this->registry->getObject('template')->buildFromTemplates( 'wall/wallmsgs.tpl.php');
								
							require_once( FRAMEWORK_PATH . 'models/Wall.php');
							$wall = new Wall($this->registry);
								
							$wall->Updates($registry->getObject('authenticate')->getUser()->getCurrentProfile());
							
							$this->registry->getObject('template')->parseOutput();
							$content = $this->registry->getObject('template')->getPage()->getContent();
							$content = '<DATA>'.$content.'</DATA>';
							$this->registry->getObject('template')->getPage()->setContent($content);
							
							
							
							echo '<loadedcount>'.$wall->getLoadedStatusCount().'</loadedcount>';
						}
						break;
					default:
						break;
				}
			}
		}
	}
}
